#ifndef COMMCONFIG_H
#define COMMCONFIG_H

#include <QDialog>
#include <QSerialPort>
#include <QSettings>
#include <QQueue>

#include "MavlinkHandler/mavlinkhandler.h"

// field
typedef struct _FIELD{
    QString name;
    double rtValue;
    int sysid;
    int msgid;
    _FIELD()
    {
        name = "";
        rtValue = 0.0;
        sysid = 0;
        msgid = 0;
    }
}FIELD,*PFIELD;

// message
typedef struct _MESSAGE{
    QString name;
    int sysid;
    int msgid;
    int nums;
    QVector<FIELD> fields;
    _MESSAGE()
    {
        name = "";
        sysid = 0;
        msgid = 0;
        nums = 0;
    }
}MESSAGE;

namespace Ui {
class CommConfig;
}

class CommConfig : public QDialog
{
    Q_OBJECT

public:
    using Callback = std::function<void(const mavlink_message_t&)>;
    enum{
        SERIAL = 0,
        UDP,
        TCP,
        REPLAY,
    };

    explicit CommConfig(QWidget *parent = 0);
    ~CommConfig();

    // Load ini file
    bool loadConfigFile(void);
    // Tcp/Udp config
    bool setConfigNet(QString ipaddr, QString port);
    bool getConfigNet(QString &ipaddr, QString &port);
    // Log config
    bool setConfigLog(QString logpath);
    bool getConfigLog(QString &logpath);
    // Comm Select
    bool setConfigCommSelect(int select);
    bool getConfigCommSelect(int &select);

    // Call-back function
    void cbMavMessage(const mavlink_message_t &msg);

    double get_mavlink_msg_info(const mavlink_message_t &msg,
                                const mavlink_message_info_t *msgInfo,
                                uint32_t dataType,
                                uint32_t i,
                                uint32_t j);


signals:
    void newSigMavMsg(MESSAGE);

private slots:
    void on_btnCommLogFile_released();
    void on_btnCommLogPath_released();
    void on_btnCommConnect_released();

private:
    // ui
    Ui::CommConfig *ui;

    // ini
    QSettings *mConfigSetting;
    QString mNetIp;
    QString mNetPort;
    QString mLogFile;
    QString mLogPath;
    int mCommSelect;

    // Thread
    bool mConnect;
    float mDropRate;
    float mNetRate;

    MavlinkHandler *mMavlinkHandler;
};

#endif // COMMCONFIG_H
